a2_task1_ingman_gabriel

Author

Gabriel Ingman

Assignment 2, Task 1

Overview

The dataset for this task is California oil spill tracking data from in 2008, stored in a CSV file, and California county borders, stored as a ShapeFile.

The purpose of this task is to analyze the oil spill tracking data and plot the spill incidents on the CA county ShapeFileas an interactive map. Then, the second part of the task is to create a cloropleth or ‘heatmap’ showing which counties were the worst affected by oil spill incidents in 2008.

I accomplished the first part of this task- the interactive oil spill incident map- in three steps. First, I converted the oil spill incident CSV into a ShapeFileso I could display the information spatially. Then, I matched up the new oil spill ShapeFile’s CRS code to the one in the CA county file so they would align on the map. Finally, I used the T_Map package to create an interactive map.

I accomplished the second part of the task by creating a merged ShapeFile with both the oil spill incident and county boundary ShapeFiles. Then, I made a new ShapeFile, storing only the total count of oil spill incidents by county. I then used a custom gradient color scheme to differentiate the low oil spill incident count counties from the ones with a high oil spill incident count.

I will be doing the optional third task.

Code
library(tidyverse)
library(here)
library(sf)
library(tmap)
library(spatstat)
library(patchwork)
Code
ca_countyborder_sf <- read_sf(here('data', 'CA_Counties'), layer = 'CA_Counties_TIGER2016') %>%
  janitor::clean_names()

oilspill_df <- read_csv(
  here('data', 'Oil_Spill_Incident_Tracking_[ds394].csv')) %>% 
  janitor::clean_names()

Exploratory Interactive Map

Code
# Converting the Oil Spill dataframe into a shapefile so it can interact with the county border shapefile

oilspillshape_sf <- oilspill_df %>% 
  drop_na(x, y) %>% 
  st_as_sf(coords = c("x", "y"))
Code
# Checking the CRS of county border file
#st_crs(ca_counties_sf) #ID  ["EPSG", 3857]

# Set Oil Spill shapefile CRS to CRS of the county border file.
st_crs(oilspillshape_sf) <- 3857
Code
tmap_mode(mode = 'view')

#Created the map with the Go Gauchos color scheme.
tm_shape(ca_countyborder_sf) +
  tm_fill(col = "cornflowerblue") +
  tm_shape(oilspillshape_sf) +
  tm_dots(col = "gold2")+
  tm_layout(title = "Oil Spills in California, 2008", inner.margins = c(0,0,.1,0), title.size = 8)

Click a point to reveal more information: date, time, location, object ID, latitude and longitude.

Static Cloropleth (Color Theme) Map

Code
#First, create a unified shapefile for both the oil spills and the CA county borders.

CAcounties_oilspills_sf <- st_join(ca_countyborder_sf, oilspillshape_sf)

#Then, count the number of oil spills in each county.

oilspill_count_sf <- CAcounties_oilspills_sf %>% 
  group_by(name) %>% 
  summarize(oil_count = n())
Code
oilspill_heatmap <- ggplot(oilspill_count_sf) +
  geom_sf(aes(fill = oil_count)) +
  scale_fill_gradientn(
    colors = c("#E8ECE4", "#ECC78F", "#da600d", "#7C1B03")
    ) +
  theme_void()+
  labs(fill = "Oil Spill Incident Count")
Code
oilspill_heatmap + plot_annotation(
  title = 'Oil Spill Cloropleth Map',
  subtitle = 'By CA County',
  caption = "The top county for oil spill incidents is Los Angeles County."
  
)